package metier.sess;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import metier.entities.Vat;

@Stateless(name = "Vat")
public class VatEJBImpl implements IvatLocal {

	@PersistenceContext(unitName = "PU_Cargaison")
	private EntityManager em;

	@Override
	public void addVat(Vat v) {
		em.persist(v);

	}

	@Override
	public List<Vat> getAllVat() {
		Query req=em.createQuery("select v from Vat v");
		return req.getResultList();
	}

	@Override
	public Vat getVat(Integer Id) {
		Vat de=em.find(Vat.class, Id);
		if(de==null)throw new RuntimeException("Vat introuvable");
		return de;
	}

	@Override
	public void updateVat(Vat v) {
		em.merge(v);
	}

	@Override
	public void deleteVat(Integer Id) {
		Vat v=getVat(Id);
		em.remove(v);
		//System.out.println("je suis laaa");
	}

	@Override
	public List<Vat> getAllActiveVats() {
		Query req=em.createQuery("select v from Vat v where v.Statut=true");
		return req.getResultList();
	}

}